<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function getUser() {
      return new Promise((resolve, reject) => {
        console.log('开始获取A数据')
        try {
          // throw new Error('A数据获取失败')
          setTimeout(() => {
            console.log('获取A数据成功')
            const user = {
              user: 'xiaoA'
            }
            resolve(user)
          }, 2000)
        } catch (e) {
          reject(e.message)
        }
      })
    }

    function getAge(user) {
      return new Promise((resolve, reject) => {
        console.log('开始获取B数据')
        try {
          // throw new Error('B数据获取失败')
          setTimeout(() => {
            console.log('获取B数据成功')
            const age = {
              age: '23'
            }
            resolve({
              ...user,
              ...age
            })
          }, 2000)
        } catch (e) {
          reject(e.message)
        }
      })
    }

    function getSex(age) {
      return new Promise((resolve, reject) => {
        console.log('开始获取C数据')
        try {
          // throw new Error('C数据获取失败')
          setTimeout(() => {
            console.log('获取C数据成功')
            const sex = {
              sex: '男'
            }
            resolve({
              ...age,
              ...sex
            })
          }, 2000)
        } catch (e) {
          reject(e.message)
        }
      })
    }

    async function getData() {
      const user = await getUser()
      const age = await getAge(user)
      const sex = await getSex(age)

      console.log('最后获取到的数据是', sex)
    }
    getData()
  </script>
</body>

</html>